// pages/index.js
const app = getApp()
import {postJSON,getJSON}  from '../../server'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paySuccess:false,
    payStatus:false,
    payPrice:0,
	payType:'',
    openId:'',
    payCode:'',
	orderNo:''
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  
    onLoad: function (options) {
        var that=this
        that.setData({
            payPrice:options.price || 0,
			payType:options.payType || '',
			orderNo:options.orderNo || '',
        })
        wx.login({
            success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            // wx.showToast({title: res.code, icon: 'none',duration: 2000 });
                console.log(res)
                that.setData({payCode:res.code})
            }
        })
    },
    pay(){
        //获取微信支付的数据
        var that =this
        var params = {code:this.data.payCode}
        getJSON('/get/openid/info',params, res => {
            if(res.data.code === "200"){
                that.setData({
                    openId:res.data.data.openid
                })
                wx.showToast({title: '获取openI成功', icon: 'none',duration: 2000 });
                that.payHaled()
            }else{
                wx.showToast({title: res.data.data, icon: 'none',duration: 2000 });
            }
        })
    },
    payHaled(){
        var that = this
		var params = {
			openId:that.data.openId,
			payType:that.data.payType,
			orderNo:that.data.orderNo
        }
        postJSON('/get/hf/pay/info',params,res=>{
            if(res.data.code === '200'){
                wx.showToast({title: '获取支付参数成功', icon: 'none',duration: 2000 });
                var wechatData = res.data.data.param
                // 成功之后调用微信支付
                wx.requestPayment({
                    'appId' : wechatData.appId,
                    'timeStamp': wechatData.timeStamp,
                    'nonceStr': wechatData.nonceStr,
                    'package': wechatData.package,
                    'signType': wechatData.signType,
                    'paySign': wechatData.paySign,
                'success':function(res){
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.setData({
                        paySuccess:true,
                        payStatus:true
                    })
                },
                'fail':function(res){
                    console.log(res);
                    console.log('fail');
                    that.setData({
                        paySuccess:false,
                        payStatus:true
                    })
                },
                'complete': function(res){
                    console.log(res);console.log('complete');
                }
                });
            }else{
                wx.showToast({title: res.data.message, icon: 'none',duration: 2000 });
            } 
        })
    },
    launchAppError(e){
        console.log("错误信息为：",e);
        wx.showToast({
            title: e.detail.errMsg,
            icon:'none'
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})